package com.ease.framework.layers {
   import com.ease.framework.figures.IFigure;
   import com.ease.framework.gef4p.constant.DiagramConstant;

   import flash.display.DisplayObject;

   import mx.collections.ArrayCollection;
   import mx.collections.Sort;
   import mx.collections.SortField;
   import mx.core.UIComponent;

   public class LaneLayer extends Layer {
      //用于记录当前lane layer中的lane允许类型.
      public var laneType:String;

      public var laneNum:int = 0;

      public function LaneLayer() {
         super();
         this.visible = true;
      }

      /**
       * 获取当前鼠标所放置的Lane对象，有别于findFigureAt方法。此方法，即使鼠标放置于空白地点，也能获取当前Lane对象.
       * @param stageX
       * @param stageY
       * @return
       *
       */
      public function findLane(stageX:Number, stageY:Number):IFigure {

         var i:int;
         //从最上层图形开始遍历
         for (i = this.numChildren - 1; i >= 0; i--) {
            if (getChildAt(i) is IFigure) {
               var childFigure:UIComponent = getChildAt(i) as UIComponent;
               if (childFigure.hitTestPoint(stageX, stageY)) {
                  return childFigure as IFigure;
               }
            }
         }
         return null;
      }

      /**
       * 获取最后的LaneFigure对象，如果是Vertical则是最下面一个，如果是Horizontal则是最右边一个
       * @return
       *
       */
      public function getLastLane():IFigure {
         var sortLanes:ArrayCollection = getSortedLanes();
         if (sortLanes.length == 0)
            return null;
         return sortLanes.getItemAt(sortLanes.length - 1) as IFigure;
      }

      public function getFirstLane():IFigure {
         return getSortedLanes().getItemAt(0) as IFigure;
      }

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         //刷新所有Lane的宽度或高度
         //主要是由于viewport变大或缩小后，对Lane视图的影响.
         var i:int;
         for (i = this.numChildren - 1; i >= 0; i--) {
            if (getChildAt(i) is IFigure) {
               var childFigure:UIComponent = getChildAt(i) as UIComponent;
//    				if(laneType==""){
//    					childFigure.width = this.width - 40;
//    				}else{
//    					childFigure.height = this.height-40;
//    				}
               childFigure.validateNow();
            }
         }
      }

      public function removeLane(laneFigure:IFigure):void {
         this.removeChild(laneFigure as DisplayObject);
         laneNum--;
      }

      /**
       * 按照当前Lane位置由上到下来排列（或由左至右排列）.
       * @return
       */
      public function getSortedLanes():ArrayCollection {
         var children:ArrayCollection = new ArrayCollection();
         var i:int;
         for (i = this.numChildren - 1; i >= 0; i--) {
            if (getChildAt(i) is IFigure) {
               children.addItem(getChildAt(i));
            }
         }
         if (children.length == 0)
            return children;

         var dataSortField_y:SortField = new SortField();
         if (laneType == DiagramConstant.LANE_TYPE_HORIZONTAL) {
            dataSortField_y.name = "y";
         }
         else {
            dataSortField_y.name = "x";
         }

         dataSortField_y.numeric = true;

         var numericDataSort:Sort = new Sort();
         numericDataSort.fields = [dataSortField_y];
         children.sort = numericDataSort;
         children.refresh();
         laneNum = children.length;
         return children;
      }
   }
}